package com.matriq.cloudmanager.fragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.CourseEvalActivity;
import com.matriq.cloudmanager.entity.CourseComment;
import com.matriq.cloudmanager.entity.TypeCourseComments;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.lgpage.fragment.BaseFragment;
import net.lgpage.http.SwipeRefeshAsyncTask;
import net.lgpage.util.Cleaner;

public class CourseEvalListFragment extends BaseFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fm_course_eval_list, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setViews();
		setEvents();
		loadData();
	}

	android.support.v4.widget.SwipeRefreshLayout srl;
	android.support.v7.widget.RecyclerView rcy_course_eval_list;
	LinearLayout ll_nothing;
	TextView tv_nothing;

	private void setViews() {
		srl = (android.support.v4.widget.SwipeRefreshLayout) getView().findViewById(R.id.srl);
		rcy_course_eval_list = (android.support.v7.widget.RecyclerView) getView()
				.findViewById(R.id.rcy_course_eval_list);
		rcy_adp = new RcyAdp();
		rcy_course_eval_list.setLayoutManager(new LinearLayoutManager(getActivity()));
		rcy_course_eval_list.setAdapter(rcy_adp);
		ll_nothing = (LinearLayout) getView().findViewById(R.id.ll_nothing);
		tv_nothing = (TextView) getView().findViewById(R.id.tv_nothing);
	}

	private void setEvents() {
		srl.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				refeshRcyList();
			}
		});
		setNotingEvents();
	}

	private void setNotingEvents() {
		ll_nothing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeListState(false);
				refeshRcyList();
			}
		});
	}

	private void checkListNoting() {
		if (rcylist == null || rcylist.size() == 0) {
			changeListState(true);
		} else {
			changeListState(false);
		}
	}

	private void changeListState(boolean isnothing) {
		if (!isnothing) {
			ll_nothing.setVisibility(View.GONE);
			srl.setVisibility(View.VISIBLE);
		} else {
			ll_nothing.setVisibility(View.VISIBLE);
			srl.setVisibility(View.GONE);
		}
	}

	private class RcyViewHolder extends RecyclerView.ViewHolder {
		TextView tv[] = new TextView[5];

		public RcyViewHolder(View view) {
			super(view);
		}
	}

	RcyAdp rcy_adp;

	class RcyAdp extends RecyclerView.Adapter<RcyViewHolder> {

		LayoutParams lp;

		@Override
		public int getItemCount() {
			if (rcylist == null)
				return 0;
			return rcylist.size();
		}

		public RcyAdp() {
			super();
			lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}

		String args[] = { "去评价", "已评价", "暂存评价", "未听课程" };

		int colors[] = { 0xffDF5A4D, 0xff80E9A6, 0xffE69D4D, 0xffE69D4D };

		@Override
		public void onBindViewHolder(RcyViewHolder h, final int position) {
			final CourseComment obj = rcylist.get(position);
			h.tv[0].setText(obj.getCourse());
			h.tv[1].setText("上课时间:" + obj.getTime());
			h.tv[2].setText("任课老师:" + obj.getTeacher());
			h.tv[3].setText("教学形式:" + obj.getType());
			try {
				h.tv[4].setText(args[obj.getStatus()]);
			} catch (Exception e1) {
				h.tv[4].setText(args[0]);
			}
			try {
				h.tv[4].setTextColor(colors[obj.getStatus()]);
			} catch (Exception e) {
				h.tv[4].setTextColor(colors[0]);
			}
			switch (obj.getStatus()) {
			case 0:
				h.itemView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent it = new Intent();
						it.setClass(getActivity(), CourseEvalActivity.class);
						it.putExtra("courseid", obj.getNum());
						it.putExtra("type", 1);
						startActivityForResult(it, 1);
					}
				});
				break;
			case 1:
				h.itemView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent it = new Intent();
						it.setClass(getActivity(), CourseEvalActivity.class);
						it.putExtra("courseid", obj.getNum());
						it.putExtra("type", 2);
						startActivity(it);
					}
				});
				break;
			case 2:
				h.itemView.setOnClickListener(null);
				break;
			case 3:
				h.itemView.setOnClickListener(null);
				break;
			default:
				h.itemView.setOnClickListener(null);
				break;
			}

		}

		@Override
		public RcyViewHolder onCreateViewHolder(ViewGroup vgp, final int position) {
			View cv = getActivity().getLayoutInflater().inflate(R.layout.rcy_course_eval_list, null);
			cv.setLayoutParams(lp);
			RcyViewHolder h = new RcyViewHolder(cv);
			h.tv[0] = (TextView) cv.findViewById(R.id.tv0);
			h.tv[1] = (TextView) cv.findViewById(R.id.tv1);
			h.tv[2] = (TextView) cv.findViewById(R.id.tv2);
			h.tv[3] = (TextView) cv.findViewById(R.id.tv3);
			h.tv[4] = (TextView) cv.findViewById(R.id.tv4);
			return h;
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == 200)
		refeshRcyList();
	};

	List<CourseComment> rcylist;
	RcyListTask rcylistTask;

	public void refeshRcyList() {
		if (rcylistTask != null) {
			rcylistTask.cancel(true);
		}
		rcylistTask = new RcyListTask(getActivity());
		rcylistTask.setSwipeLayout(srl);
		rcylistTask.execute();
	}

	private class RcyListTask extends SwipeRefeshAsyncTask<TypeCourseComments> {

		public RcyListTask(Context context) {
			super(context);
		}

		@Override
		public TypeCourseComments doInBackgroundX(String... params) throws Exception {
			return AccountService.getService().course_comment();
		}

		@Override
		public void onPostExecuteX(TypeCourseComments result) {
			if (result == null) {
				return;
			} else {
				rcylist = new ArrayList<>();
				List<CourseComment> list = result.getDispre();
				Collections.sort(list, comparator);
				for (int i = 0; i < list.size(); i++) {
					CourseComment obj = list.get(i);
					obj.setStatus(0);
					rcylist.add(obj);
				}
				list = result.getDisaft();
				Collections.sort(list, comparator);
				for (int i = 0; i < list.size(); i++) {
					CourseComment obj = list.get(i);
					obj.setStatus(1);
					rcylist.add(obj);
				}

				/*list = result.getDisdone();
				Collections.sort(list, comparator);
				for (int i = 0; i < list.size(); i++) {
					CourseComment obj = list.get(i);
					obj.setStatus(2);
					rcylist.add(obj);
				}*/
				list = result.getNO();
				Collections.sort(list, comparator);
				for (int i = 0; i < list.size(); i++) {
					CourseComment obj = list.get(i);
					obj.setStatus(3);
					rcylist.add(obj);
				}
				checkListNoting();
				rcy_adp.notifyDataSetChanged();
			}
		}
	}

	SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	Comparator<CourseComment> comparator = new Comparator<CourseComment>() {

		@Override
		public int compare(CourseComment lhs, CourseComment rhs) {
			Date ld = null;
			Date rd = null;
			try {
				ld = f.parse(lhs.getTime());
			} catch (ParseException e) {
				ld = null;
			}
			try {
				rd = f.parse(rhs.getTime());
			} catch (ParseException e) {
				rd = null;
			}
			if (ld == null && rd == null)
				return 0;
			if (ld == null)
				return 1;
			if (rd == null)
				return 0;
			return rd.compareTo(ld);
		}
	};

	private void loadData() {
		refeshRcyList();
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(rcylistTask);
		super.onDestroy();
	}

}